// 引入日历工具
import Calendar from '../../utils/calendar'




// pages/calendar/calendar.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateList: [],
    scheduleList: [],
    months: [
      '一月',
      '二月',
      '三月',
      '四月',
      '五月',
      '六月',
      '七月',
      '八月',
      '九月',
      '十月',
      '十一月',
      '十二月'
    ],
    targetMonths: 0,
    targetDate: {},
    // 是否展示弹出框
    showPanel: false,


    // 当前年份
    currentYear: -1,
    // 当前月份
    currentMonth: -1,
  },

  // 点击日期事件
  handelTapDate(e) {
    // id 为下标
    const {
      id
    } = e.currentTarget.dataset
    this.setData({
      targetDate: this.data.dateList[id],
      showPanel: true
    })
  },

  handelTapAddExit(e) {
    this.setData({
      showPanel: false
    })
  },

  // 加载日历
  loadCalendar(year, month) {
    let dateList = Calendar().customDate(year, month)
    // 获取缓存中的数据
    let scheduleList = wx.getStorageSync('scheduleList')
    // 加工日历，将同一日的进程加入到日历数组中
    let i = 0
    if (scheduleList != '') {
      dateList.map(item => {
        let date = item.date
        let month = item.month
        let year = item.year
        let thisDay = new Date(`${year}-${month}-${date}`)
        let day = thisDay.getDay()
        // 处理todoList
        let todoList = scheduleList.filter(todo => {
          todo.color = Math.round(Math.random() * 11) + 1;
          // 日循环
          if (todo.circulation == 'daily') return true;
          // 周循环
          if (todo.circulation == 'weekly') {
            return new Date(todo.date).getDay() == day
          } else {
            return new Date(todo.date).toDateString() == thisDay.toDateString()
          }
        })
        if (todoList.length != 0) {
          item.todoList = todoList
        }
        item.id = i++
        return item
      })
    }
    // 设置 日期
    let targetMonths = new Date().getUTCMonth()
    // 设置 data
    this.setData({
      dateList,
      scheduleList,
      targetMonths
    })
  },

  // 上个月
  preMonth() {
    let year, month = -1
    month = this.data.currentMonth
    year = this.data.currentYear
    if (month == 1) {
      month = 12
      year--
    } else {
      month--
    }

    this.setData({
      currentMonth: month,
      currentYear: year
    })

    this.loadCalendar(year, month)
  },

  // 下个月 
  nextMonth() {
    let year, month = -1
    month = this.data.currentMonth
    year = this.data.currentYear
    if (month == 12) {
      month = 1
      year++
    } else {
      month++
    }

    this.setData({
      currentMonth: month,
      currentYear: year
    })

    this.loadCalendar(year, month)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let nowDate = new Date()
    // 获取日历
    this.setData({
      currentYear: nowDate.getFullYear(),
      currentMonth: nowDate.getMonth() + 1
    })
    this.loadCalendar(nowDate.getFullYear(), nowDate.getMonth() + 1)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})